package com.fengquan.slicing.controller;

import com.fengquan.slicing.application.IFileApplication;
import com.fengquan.slicing.dto.XmlData;
import com.fengquan.slicing.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/************************************************************
 * 作    者: P622311(冯泉)
 * 日    期: 2021/3/18 12:05
 * 用途说明: (dome)
 ************************************************************/
@Slf4j
@RestController
@RequestMapping("file")
@Api(tags = "分片上传工具类")
public class FileController {

    @Autowired
    private IFileApplication application;

    @ApiOperation(value = "合流", notes = "合流")
    @PostMapping("confluence")
    public Result confluence(@RequestBody String md5) {
        try {
            application.confluence(md5);
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.SUCCESS;
    }

    /**
     * 上传附件
     *
     * @return
     */
    @ApiOperation(value = "上传附件", notes = "上传附件")
    @PostMapping("update")
    public Result update(HttpServletRequest request, @RequestParam("content") MultipartFile file) {
        MultipartHttpServletRequest parmas=((MultipartHttpServletRequest)request);
        try {
            XmlData data=new XmlData();
            data.setId(request.getParameter("id"));
            data.setFileName(request.getParameter("fileName"));
            data.setIndex(Integer.parseInt(request.getParameter("index")));
            data.setLength(Integer.parseInt(request.getParameter("length")));
            data.setMd5(request.getParameter("md5"));
            String[] sss=request.getParameter("list").split(",");
            List<String> list=new ArrayList<>();
            for(int i=0;i<sss.length;i++){
                list.add(sss[i]);
            }
            data.setList(list);
            Map map=application.update(data,file);

            if(map.get("index").toString().equals("-1")){
                return new Result(201,"此文件已上传完");
            }
            return new Result(list);
        } catch (Exception e) {
            log.info(e.getMessage());
            return new Result(201,e.getMessage());
        }
    }

    /**
     * 下载文件信息
     * @param md5
     * @return
     */
    @ApiOperation(value = "下载文件信息", notes = "下载文件信息")
    @PostMapping("fileInfo")
    public Result fileInfo(@RequestBody String md5){
        return new Result(application.fileInfo(md5));
    }

    /**
     * 块下载
     * mongo下载
     * @return
     */
    @ApiOperation(value = "块下载 mongo下载", notes = "块下载")
    @PostMapping("download")
    public void download3(@RequestBody String md5, HttpServletRequest request, HttpServletResponse response) {
        try {
            application.download3(md5,request,response);
        } catch (Exception e) {
            log.info(e.getMessage());
        }
    }

}
